package com.horay.springbootdemo.data.source.multiple.configuration;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * 数据源配置
 *
 * @author heyingcheng
 * @date 2019/10/31 21:31
 */
@Slf4j
@Configuration
public class FooDataSourceConfiguration {

    @Bean
    @ConfigurationProperties("spring.foo.datasource")
    public DataSourceProperties fooDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    public DataSource fooDataSource(DataSourceProperties fooDataSourceProperties) {
        log.info("foo.datasource.url:[{}]", fooDataSourceProperties.getUrl());
        return fooDataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Bean
    public JdbcTemplate fooJdbcTemplate(DataSource fooDataSource) {
        return new JdbcTemplate(fooDataSource);
    }

    @Bean
    public PlatformTransactionManager fooTransactionManager(DataSource fooDataSource) throws Exception {
        return new DataSourceTransactionManager(fooDataSource);
    }

}
